///********************************************************************
///* Copyright © 2012 fishr (fishr.flash@gmail.com)  
///********************************************************************


package su.fishr.social_network.OK.uploadV2.requests 
{
	
		import flash.net.URLRequest;
		import flash.net.URLRequestHeader;
		import flash.net.URLRequestMethod;
		import flash.utils.ByteArray;
		import su.fishr.social_network.OK.uploadV2.utils.PostRequest;
		import su.fishr.utils.Dumper;
	
   

	/** 
	 * @private 
     * 
	 * Непосредственно загружает фотографию.
	 * Реализует запрос photosV2 - photo upload via HTTP POST
	 * 
	 * @playerversion          Flash 10
	 * @langversion            3.0
	 * @author                 fishr
	 * @created                30.05.2012 7:32
	 * @since                  30.05.2012 7:32
	 */
	public class UploadPhoto extends Requester
	{
	/**-------------------------------------------------------------------------------
	* 
	*	   						V A R I A B L E ' S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		static private const AUTHOR:String = "fishr (fishr.flash@gmail.com)";
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*	 						P R O P E R T I E S 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
	
	//}
	/**-------------------------------------------------------------------------------
	* 
	*								P U B L I C 	
	* 
	* --------------------------------------------------------------------------------
	*/
	//{
		/**
		 * Конструктор.
		 * 
		 * @param	callback
		 * @param	errorCall
		 * @param	url
		 * @param	photo
		 * @param	photoName
		 * @param	request
		 */
		public function UploadPhoto(callback:Function, errorCall:Function, url:String, photo:ByteArray, photoName:String = "getImage", request:URLRequest = null)
		{
			super(callback, errorCall, request);
			
			init( url, photo, photoName );
		}
		
		
	//}
	
	/**-------------------------------------------------------------------------------
	* 
	*								P R I V A T E 	
	* 
	* --------------------------------------------------------------------------------
	*/	
	//{
		private function init(url:String, photo:ByteArray, photoName:String):void 
		{
			var request:URLRequest = new URLRequest( url );
			
			const post:PostRequest = new PostRequest();
			post.writeFileData( photoName, photo, "image/jpeg" );
			post.close();
			photo = null;
			
			request.method = URLRequestMethod.POST;
			request.data = post.getPostData();
			request.requestHeaders.push(new URLRequestHeader('Content-Type', 'multipart/form-data; boundary=' + post.boundary));
			
			super.load( request );
		}
	//}
		
		
	}

}